#! /bin/sh

source /etc/rc.d/common

IB_CONFIG="/etc/infiniband/openib.conf"
IB_SYSFS="/sys/class/infiniband"

check_args ${@}

# Make sure the IB configuration file exists
if [ ! -f "${IB_CONFIG}" ]; then
    /bin/echo "No InfiniBand configuration found!"
    exit 0
fi

# Load configuration
. ${IB_CONFIG}
KERN_MODS=""
if [ "x${MTHCA_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} ib_mthca"
fi
if [ "x${MLX4_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} mlx4_ib"
fi
if [ "x${MLX5_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} mlx5_ib"
fi
if [ "x${QIB_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} ib_qib"
fi
if [ "x${CXGB3_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} iw_cxgb3"
fi
if [ "x${CXGB4_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} iw_cxgb4"
fi
if [ "x${I40IW_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} i40iw"
fi
if [ "x${OCRDMA_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} ocrdma"
fi
if [ "x${IPOIB_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} ib_ipoib"
fi
if [ "x${SRP_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} ib_srp"
fi
if [ "x${ISER_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} ib_iser"
fi
if [ "x${RDMA_UCM_LOAD}" == "xyes" ]; then
    KERN_MODS="${KERN_MODS} rdma_ucm"
fi

start() {
    # Load the modules
    /bin/echo "Loading configured IB modules..."
    for i in ${KERN_MODS}; do
        /sbin/modprobe ${i} || exit 1
    done
    /sbin/modprobe ib_umad || exit 1
    /sbin/modprobe ib_uverbs || exit 1
    /sbin/modprobe rdmavt || exit 1
    # Add node description to sysfs
    if [ -d ${IB_SYSFS} ]; then
        hca_id=1
        for hca in ${IB_SYSFS}/*; do
            if [ -e ${hca}/node_desc ]; then
                /usr/bin/logger "Set node_desc for $(/usr/bin/basename $hca): $(/bin/hostname -s) HCA-${hca_id}"
                echo -n "$(/bin/hostname -s) HCA-${hca_id}" >> ${hca}/node_desc
            fi
            hca_id=$(/usr/bin/expr ${hca_id} + 1)
        done
    fi
    # Enable connected mode for IPoIB interfaces
    if [ "x${IPOIB_LOAD}" == "xyes" ]; then
        interfaces=$(cd /sys/class/net; /bin/ls -d ib* 2> /dev/null)
        for i in ${interfaces}; do
            if [ "x${SET_IPOIB_CM}" == "xyes" ]; then
                if [ ! -e /sys/class/net/${i}/mode ]; then
                    /bin/echo "Failed to configure IPoIB connected mode for ${i}."
                fi
                /bin/sleep 1
                /bin/echo connected > /sys/class/net/${i}/mode
            fi
        done
    fi
    # A delay here is required for some HCA's
    /bin/echo "Waiting for any physical links to establish..."
    /bin/sleep 10
}

stop() {
    # Unload the modules
    /bin/echo "Unloading configured IB modules..."
    for i in $(/bin/echo ${KERN_MODS} | /usr/bin/tr ' ' '\n' | \
        /usr/bin/tac | /usr/bin/tr '\n' ' '); do
        /sbin/rmmod -w ${i}
    done
    /sbin/rmmod -w rdmavt
    /sbin/rmmod -w ib_uverbs
    /sbin/rmmod -w ib_umad
}

status() {
    # We don't know anything
    exit ${UNKNOWN}
}

# Perform specified action
${1}
